// pages/tailPurchasing/tailPurchasing.js

const app = getApp();
import httpsUtils from '../../utils/https.js';
import myUtils from '../../utils/util.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList: [],
    current: 1,
    hasNextPage: true,
    isEmpty: false,
  },
  /**
  * 获取商品
  */
  getGoods: function (e) {
    wx.showLoading({
      title: '加载中...',
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 8000)
    var that = this;
    var userId = wx.getStorageSync("userInfo").id;
    if (myUtils.isBlank(userId) || userId == undefined){
      wx.hideLoading();
      wx.showModal({
        title: '登陆提醒',
        content: '需要您登陆才能查看',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../wxLogin/wxLogin?type=7&isTab=false',
            })
          } else {
            wx.switchTab({
              url: '../myinfo/myinfo',
            })
          }
        }
      })
    }else{
      httpsUtils.getData({
        _url: app.globalData._url + '/tailGoods/selectList?isXcx=1&sort=2&current=' + that.data.current + "&userId=" + userId,
        data: '',
      }, function (json) {
        if (json.success == false && json.code == 'OVERTIME') {
          wx.showModal({
            title: '登陆提醒',
            content: '需要您登陆才能查看',
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '../wxLogin/wxLogin?type=7&isTab=false',
                })
              } else {
                wx.switchTab({
                  url: '../myinfo/myinfo',
                })
              }
            }
          })
        } else if (json.data && json.success) {
          var goodsList = that.data.goodsList.concat(json.data.list);
          var isEmpty = false;
          if (goodsList.length == 0) {
            isEmpty = true;
          }
          that.setData({
            goodsList: goodsList,
            current: json.data.pageNum,
            hasNextPage: json.data.hasNextPage,
            isEmpty: isEmpty
          })
        }
        wx.hideLoading();
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  // 事件函数

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.setData({
      goodsList: [],
      current: 1,
      hasNextPage: true,
    })
    //获取商品
    that.getGoods();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.hasNextPage) {
      that.setData({
        current: that.data.current + 1,
      })
      that.getGoods();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '这些尾货特价，谁要？点开联系我',
      path: 'pages/askToBuyTailShare/askToBuyTailShare?id=' + wx.getStorageSync("userInfo").id,
    }
  },

  /**
    * 生命周期函数--监听页面加载
    */
  onPageScroll: function (event) {
  },

})